package com.wdz;

import com.alibaba.fastjson.JSONObject;
import com.wdz.http.Response;
import com.wdz.utils.TokenUtils;
import com.wdz.websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;
import java.io.IOException;
import java.util.Set;

/**
 * @EnableZuulProxy - 开启Zuul网关。
 *  当前应用是一个Zuul微服务网关。会在Eureka注册中心中注册当前服务。并发现其他的服务。
 *  Zuul需要的必要依赖是spring-cloud-starter-zuul。
 */
@EnableZuulProxy
/*@EnableDiscoveryClient*/
@EnableScheduling
@EnableFeignClients
@Configuration
@SpringBootApplication()
public class BkHtCenterApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(BkHtCenterApplication.class, args);

        Environment environment = context.getBean(Environment.class);
        System.out.println("启动成功 ！ " +environment.getProperty("server.port"));
        System.out.println("\n" +
                ".____    .__         __      __             _____.___.__                 .___  .____    ____________   _______________ _____.___.________   ____ ___\n" +
                "|    |   |__|__ __  /  \\    /  \\ ____   ____\\__  |   |__| ____    ____   |   | |    |   \\_____  \\   \\ /   /\\_   _____/ \\__  |   |\\_____  \\ |    |   \\\n" +
                "|    |   |  |  |  \\ \\   \\/\\/   // __ \\ /    \\/   |   |  |/    \\  / ___\\  |   | |    |    /   |   \\   Y   /  |    __)_   /   |   | /   |   \\|    |   /\n" +
                "|    |___|  |  |  /  \\        /\\  ___/|   |  \\____   |  |   |  \\/ /_/  > |   | |    |___/    |    \\     /   |        \\  \\____   |/    |    \\    |  /\n" +
                "|_______ \\__|____/    \\__/\\  /  \\___  >___|  / ______|__|___|  /\\___  /  |___| |_______ \\_______  /\\___/   /_______  /  / ______|\\_______  /______/\n" +
                "        \\/                 \\/       \\/     \\/\\/              \\//_____/                 \\/       \\/                 \\/   \\/               \\/\n");
    }

    @Autowired
    private TokenUtils tokenUtils;

    //每隔一个小时检查是否有过期的token
    @Scheduled(cron = "0 0 */1 * * ?")
    public void fixedRate() {
        System.out.println("定时任务启动");
        Response response = new Response(401,0,"用户信息已失效，请重新登录！",null);
        Set<String> ids = WebSocket.webSocketSet.keySet();
        try {
            for (String item : ids) {
                if (tokenUtils.isTokenExpired(item)) {
                    WebSocket.webSocketSet.get(item).sendMessage(response.toJsonString());
                    WebSocket.webSocketSet.get(item).onClose();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 文件上传配置
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //单个文件最大
        factory.setMaxFileSize(DataSize.parse("512000KB"));
        /// 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.parse("512000KB"));

        return factory.createMultipartConfig();
    }
    @Value("${spring.profiles.active}")
    private String tenant;
    @Autowired
    private WebSocket webSocket;
    //3.添加定时任务
    //@Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔，例如：14秒
    @Scheduled(fixedRate=5*1000)
    private void configureTasks() throws Exception{
        if("pro".equals(tenant)) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", 0);
            jsonObject.put("value", "ping");
            webSocket.sendAll(jsonObject.toJSONString());
        }
    }
}
